package com.zjq.authoritysystem.controller.permission;

import com.zjq.authoritysystem.entity.Permission;
import com.zjq.authoritysystem.service.PermissionService;
import com.zjq.authoritysystem.vo.query.PermissionQueryVo;
import com.zjq.authoritysystem.vo.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author BetOnMe
 * @version 1.0
 * @ClassName TestController
 * @description: TODO
 * @since 2023年09月18日 22:35:29
 */
@Slf4j
@Api(tags = "菜单权限控制器")
@RestController
@RequestMapping("/api/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @ApiOperation("查询所有菜单权限信息")
    @GetMapping("/list")
    public Result list(PermissionQueryVo permissionQueryVo) {
        List<Permission> roles = permissionService.listByTreeModel(permissionQueryVo);
        return Result.SUCCESS(roles);
    }

    @ApiOperation("保存菜单权限信息")
    @PostMapping("/save")
    public Result save(@RequestBody Permission permission) {
        boolean saveOrUpdate = permissionService.saveOrUpdate(permission);
        if (!saveOrUpdate) {
            return Result.Error("保存失败！");
        }
        return Result.SUCCESS(true);
    }

    @ApiOperation("删除菜单权限信息")
    @PostMapping("/delete/{id}")
    public Result delete(@ApiParam(name = "id", value = "菜单信息编号", required = true)
                         @PathVariable Integer id) {
        boolean removeById = permissionService.removeById(id);
        if (!removeById) {
            return Result.Error("删除失败");
        }
        return Result.SUCCESS(true);
    }

    @ApiOperation("检查是否含有子菜单")
    @GetMapping("/checkPermission/{id}")
    public Result checkPermission(@ApiParam(name = "id", value = "菜单信息编号")
                                  @PathVariable Integer id) {
        Boolean check = permissionService.checkPermissionHaveChildren(id);
        if (check) {
            return Result.Error("该菜单下面存在子元素，清除子元素后才可删除该菜单！");
        }
        return Result.SUCCESS();
    }

    @ApiOperation("根据编号获取菜单权限信息")
    @GetMapping("/get/{id}")
    public Result get(
            @ApiParam(name = "id", value = "菜单信息编号")
            @PathVariable Integer id) {
        Permission permission = permissionService.getById(id);
        return Result.SUCCESS(permission);
    }

}
